package com.cjfather.redis.steam.annotation.consumers;

import java.util.HashMap;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.data.redis.connection.stream.MapRecord;
import org.springframework.data.redis.connection.stream.RecordId;
import org.springframework.stereotype.Component;

import com.cjfather.redis.steam.annotation.AnnoRedisStreamListener;
import com.cjfather.redis.steam.annotation.constants.RedisConstants;
import com.cjfather.redis.steam.controller.PropellingInfo;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
 * 
 * @author: todd
 * @date:   2023-08-08
 */

@Component
@Slf4j
@Scope("prototype")
@Data
public class TestMapPojoStreamConsumer extends RedisTemplateResource implements AnnoMapPojoConsumerI{
	/**
     * 消费组
     */
    private String group;
    /**
     * 消费组中的某个消费者
     */
    private String consumerName;
    
	private Gson gson = new GsonBuilder().create();


	@AnnoRedisStreamListener(streamKey = RedisConstants.FIRE_STREAM_KEY, consumerGroup = RedisConstants.FIRE_STREAM_GROUP, consumerName = RedisConstants.FIRE_CONSUMER_NAME,consumerSize = 2)
	@Override
	public void onMessage(MapRecord<String, Object, Object> message) {
		PropellingInfo info = gson.fromJson(gson.toJson(message.getValue()), PropellingInfo.class);
		log.info("{}",info);
		log.info("{}",gson.toJson(message));
		String key = message.getStream();
		RecordId messageId = message.getId();
		Map<Object, Object> value = message.getValue();
		String vv = gson.toJson(value.get("data")).replaceAll("\\\\", "");
		Map<String, Object> value2 = new HashMap<String,Object>();
		String[] ts = {"todd"};
		value2.put("todd", "2.0");
		value2.put("test", ts);
//		value2.replaceAll((k, v) -> String.valueOf(v));
		stringRedisTemplate.opsForStream().add(key + "todd", value2);
		Map<String, Object> msgMain = gson.fromJson(vv, Map.class);
//		redisStreamCommonService.delRecord(key,messageId);;
		log.info("key:{},组:{},消费者{} + stream message。messageId={},  body={}",key, group,consumerName,messageId, msgMain);
	}

}
